package xyf.p04.tree;

import commons.TreeNode;

/**
 * ClassName: P0543_TreeDiameter
 * Description:
 * Author: xyf
 * Date: 9/8/22 5:59 PM
 * Version: 1.0
 **/
public class P0543_TreeDiameter
{
    private int max = 0;

    public int diameterOfBinaryTree(TreeNode root)
    {
        return dfs(root);
    }

    //深度
    private int dfs(TreeNode root)
    {
        if (root == null)
        {
            return 0;
        }

        int leftDiameter = dfs(root.left);
        int rightDiameter = dfs(root.right);
        max = Math.max(max, leftDiameter + rightDiameter);
        return Math.max(leftDiameter, rightDiameter) + 1;
    }
}